package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//http://127.0.0.1:8202/swagger-ui.html
@Api(tags = "医院数据字典")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    private DictService dictService;

    //根据项目id，查询字典子数据列表
    @ApiOperation("根据id，查询字典子数据列表")
    @GetMapping("findChirdData/{id}")
    public Result queryChirdrenDataById(@PathVariable Long id){
        List<Dict> dictList = dictService.findChirdData(id);
        return Result.ok(dictList);
    }

    //根据DictCode，查询字典子数据列表：获取下级节点
    @ApiOperation("根据DictCode，查询字典子数据列表")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return Result.ok(dictList);
    }

    //下载数据字典
    @GetMapping("downloadDictToExcel")
    public void downloadDictToExcel(HttpServletResponse response)  {
        dictService.downloadDict(response);
    }

    //上传数据字典
    @PostMapping("importDictToBase")
    public Result importDictToBase(MultipartFile file){
        dictService.importDict(file);
        return Result.ok();
    }

    //获取数据字典名称(双参数)
    @GetMapping("getDictName/{dictCode}/{value}")
    public String getDictName(@PathVariable String dictCode,@PathVariable String value){
        return dictService.getDictName(dictCode,value);
    }
    //获取数据字典名称(单参数)
    @GetMapping("getDictName/{value}")
    public String getDictName(@PathVariable String value){
        return dictService.getDictName("",value);
    }



}
